package com.ruoyi.scale.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scale.domain.ScaleCategory;
import com.ruoyi.scale.service.IScaleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 量表分类Controller
 *
 * @author dais
 * @date 2023-09-03
 */
@RestController
@RequestMapping("/scale/category")
public class ScaleCategoryController extends BaseController {
    @Autowired
    private IScaleCategoryService scaleCategoryService;

    public ScaleCategoryController() {
        System.out.println();
    }

    /**
     * 查询量定义列表
     */
    @PreAuthorize("@ss.hasPermi('scale:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScaleCategory scaleCategory) {
        startPage();
        List<ScaleCategory> list = scaleCategoryService.selectScaleCategoryList(scaleCategory);
        return getDataTable(list);
    }

    /**
     * 导出量定义列表
     */
    @PreAuthorize("@ss.hasPermi('scale:category:export')")
    @Log(title = "量定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScaleCategory scaleCategory) {
        List<ScaleCategory> list = scaleCategoryService.selectScaleCategoryList(scaleCategory);
        ExcelUtil<ScaleCategory> util = new ExcelUtil<ScaleCategory>(ScaleCategory.class);
        util.exportExcel(response, list, "量定义数据");
    }

    /**
     * 获取量定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('scale:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(scaleCategoryService.selectScaleCategoryByCategoryId(categoryId));
    }

    /**
     * 新增量定义
     */
    @PreAuthorize("@ss.hasPermi('scale:category:add')")
    @Log(title = "量定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScaleCategory scaleCategory) {
        return toAjax(scaleCategoryService.insertScaleCategory(scaleCategory));
    }

    /**
     * 修改量定义
     */
    @PreAuthorize("@ss.hasPermi('scale:category:edit')")
    @Log(title = "量定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScaleCategory scaleCategory) {
        return toAjax(scaleCategoryService.updateScaleCategory(scaleCategory));
    }

    /**
     * 删除量定义
     */
    @PreAuthorize("@ss.hasPermi('scale:category:remove')")
    @Log(title = "量定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(scaleCategoryService.deleteScaleCategoryByCategoryIds(categoryIds));
    }
}